/**   
 * @Title: FtpActivity.java 
 * @Package android.framework.app 
 * @Description: TODO
 * @author jtf@163.com   
 * @date 2013-7-23 下午4:30:48 
 * @version V1.0   
 */
package android.framework.app;

import java.io.FileNotFoundException;
import java.io.IOException;

import it.sauronsoftware.ftp4j.FTPAbortedException;
import it.sauronsoftware.ftp4j.FTPClient;
import it.sauronsoftware.ftp4j.FTPDataTransferException;
import it.sauronsoftware.ftp4j.FTPDataTransferListener;
import it.sauronsoftware.ftp4j.FTPException;
import it.sauronsoftware.ftp4j.FTPIllegalReplyException;
import it.sauronsoftware.ftp4j.FTPReply;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

import android.framework.R;

/**
 * @ClassName: FtpActivity
 * @Description: TODO
 * @author jtf19@163.com
 * @date 2013-7-23 下午4:30:48
 * 
 */
public class FtpActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_ftp);
		findViewById(R.id.button1).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				getFtpCLient();
				// FTPClient client = getFtpCLient();
				// try {
				// client.download("IMG_20130329_112044.png",
				// new java.io.File("/mnt/sdcard/localFile.png"),
				// new FtpListener("download ==== "));
				// } catch (IllegalStateException e) {
				// // TODO Auto-generated catch block
				// e.printStackTrace();
				// } catch (FileNotFoundException e) {
				// // TODO Auto-generated catch block
				// e.printStackTrace();
				// } catch (IOException e) {
				// // TODO Auto-generated catch block
				// e.printStackTrace();
				// } catch (FTPIllegalReplyException e) {
				// // TODO Auto-generated catch block
				// e.printStackTrace();
				// } catch (FTPException e) {
				// // TODO Auto-generated catch block
				// e.printStackTrace();
				// } catch (FTPDataTransferException e) {
				// // TODO Auto-generated catch block
				// e.printStackTrace();
				// } catch (FTPAbortedException e) {
				// // TODO Auto-generated catch block
				// e.printStackTrace();
				// } finally {
				// try {
				// client.disconnect(true);
				// } catch (IllegalStateException e) {
				// // TODO Auto-generated catch block
				// e.printStackTrace();
				// } catch (IOException e) {
				// // TODO Auto-generated catch block
				// e.printStackTrace();
				// } catch (FTPIllegalReplyException e) {
				// // TODO Auto-generated catch block
				// e.printStackTrace();
				// } catch (FTPException e) {
				// // TODO Auto-generated catch block
				// e.printStackTrace();
				// }
				//
				// }

			}
		});

		findViewById(R.id.button2).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				getFtpCLient();
				// FTPClient client = getFtpCLient();
				// try {
				// client.upload(
				// new java.io.File("/mnt/sdcard/localFile.png"),
				// new FtpListener("upload ==== "));
				// } catch (IllegalStateException e) {
				// // TODO Auto-generated catch block
				// e.printStackTrace();
				// } catch (FileNotFoundException e) {
				// // TODO Auto-generated catch block
				// e.printStackTrace();
				// } catch (IOException e) {
				// // TODO Auto-generated catch block
				// e.printStackTrace();
				// } catch (FTPIllegalReplyException e) {
				// // TODO Auto-generated catch block
				// e.printStackTrace();
				// } catch (FTPException e) {
				// // TODO Auto-generated catch block
				// e.printStackTrace();
				// } catch (FTPDataTransferException e) {
				// // TODO Auto-generated catch block
				// e.printStackTrace();
				// } catch (FTPAbortedException e) {
				// // TODO Auto-generated catch block
				// e.printStackTrace();
				// } finally {
				// try {
				// client.disconnect(true);
				// } catch (IllegalStateException e) {
				// // TODO Auto-generated catch block
				// e.printStackTrace();
				// } catch (IOException e) {
				// // TODO Auto-generated catch block
				// e.printStackTrace();
				// } catch (FTPIllegalReplyException e) {
				// // TODO Auto-generated catch block
				// e.printStackTrace();
				// } catch (FTPException e) {
				// // TODO Auto-generated catch block
				// e.printStackTrace();
				// }
				//
				// }

			}
		});
	}

	class FtpListener implements FTPDataTransferListener {

		String tag;

		public FtpListener(String tag) {
			super();
			this.tag = tag;
		}

		@Override
		public void transferred(int arg0) {
			// TODO Auto-generated method stub
			System.out.println(tag + arg0);
		}

		@Override
		public void started() {
			// TODO Auto-generated method stub
			System.out.println(tag + "start");
		}

		@Override
		public void failed() {
			// TODO Auto-generated method stub
			System.out.println(tag + " failed");
		}

		@Override
		public void completed() {
			// TODO Auto-generated method stub
			System.out.println(tag + "complete");
		}

		@Override
		public void aborted() {
			// TODO Auto-generated method stub
			System.out.println(tag + "aborted");

		}

	}

	private Handler mHandle = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 0:
				Toast.makeText(getApplicationContext(), " login  " + msg.obj, 1)
						.show();
				break;

			default:
				break;
			}
		};
	};

	private void getFtpCLient() {
		new Thread(new Runnable() {

			@Override
			public void run() {
				FTPClient client = new FTPClient();
				client.setAutoNoopTimeout(5000L);
				try {
					client.connect("manatee.vicp.cc");
					client.login("mshare", "star");
					Message msg = mHandle.obtainMessage(0, client.isConnected());
					mHandle.sendMessage(msg);
					client.disconnect(true);
				} catch (IllegalStateException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (FTPIllegalReplyException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (FTPException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}).start();
	}
}
